package com.agua.enturno.vista.paginas.adm.agendas.jornadas;

import com.agua.enturno.vista.dto.JornadaLaboralOcasionalDTO;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.joda.time.DateTime;
import org.joda.time.Duration;

import java.util.List;


public class JornadasOcasionalesPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	private IModel<List<JornadaLaboralOcasionalDTO>> model;
	private IModel<Long> modelAgendaId;
	
	public JornadasOcasionalesPanel(String id, IModel<List<JornadaLaboralOcasionalDTO>> model, IModel<Long> modelAgendaId) {
		super(id, model);
		this.setOutputMarkupId(true);
		this.model = model;
		this.modelAgendaId = modelAgendaId;
		
		add(new ListView<JornadaLaboralOcasionalDTO>("jornada-ocasional", model) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<JornadaLaboralOcasionalDTO> item) {
				item.add(new JornadaOcasionalPanel("jornada-ocasional-interna", new PropertyModel<JornadaLaboralOcasionalDTO>(item, "modelObject")));
			}
			
		});
		
		add(new AjaxFallbackLink<String>("agregar-jornada-ocasional") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				agregarJornadaOcasional();
				target.add(JornadasOcasionalesPanel.this.getParent());
			}
		});
		
	}
	
	private void agregarJornadaOcasional() {
		
		DateTime start = DateTime.now().plusDays(1).withTime(9, 0, 0, 0);
		DateTime end = DateTime.now().plusDays(1).withTime(18, 0, 0, 0);
		
		this.model.getObject().add(new JornadaLaboralOcasionalDTO(modelAgendaId.getObject(), null, start, end, Duration.standardHours(1), 1));
	}

}
